﻿<%@ WebHandler Language="C#" Class="EditInline" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using LdExpress;

public class EditInline : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        int nPermissions = 0;
        if (context.Session["logined_id"] == null)
        {
            Output("ACCESS DENIED", context);
            return;
        }

        var action = context.Request.Params["act"].ToLower();
        switch (action)
        {
            case "carrcontact":
                if (context.Session["CarrierSales"] != "") nPermissions = int.Parse(context.Session["CarrierSales"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                string vrs = context.Request.Params["version"];
                if (string.IsNullOrEmpty(vrs))
                {
                    UpdateCarrConact(context);
                }
                else if (vrs == "2")
                {
                    UpdateCarrConactV2(context);
                }
                break;
            case "colloader":
                if (context.Session["coloader"] != "") nPermissions = int.Parse(context.Session["coloader"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                string cldver = context.Request.Params["version"];
                if (string.IsNullOrEmpty(cldver))
                {
                    UpdateColLoader(context);
                }
                else if (cldver == "2")
                {
                    UpdateColLoaderV2(context);
                }
                break;
            case "localcost":
                if (context.Session["LocalCharges"] != "") nPermissions = int.Parse(context.Session["LocalCharges"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                UpdateLocalCost(context);
                break;

            case "funds":
                if (context.Session["funds"] != "") nPermissions = int.Parse(context.Session["funds"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                UpdateFunds(context);
                break;

            case "borrowbook":
                if (context.Session["borrowbook"] != "") nPermissions = int.Parse(context.Session["borrowbook"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                UpdateBorrowBook(context);
                break;
            case "receivablesreport":
                if (context.Session["receivablesreport"] != "") nPermissions = int.Parse(context.Session["receivablesreport"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                UpdateReceivablesReport(context);
                break;
            case "payablesreport":
                if (context.Session["payablesreport"] != "") nPermissions = int.Parse(context.Session["payablesreport"].ToString());
                if (nPermissions < 3)
                {
                    Output("ACCESS DENIED", context);
                    return;
                }
                UpdatePayablesReport(context);
                break;
        }
    }

    private void UpdateCarrConact(HttpContext context)
    {
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string Carr = context.Request.Params["Carr"].Trim();
        string Port = context.Request.Params["Port"].Trim();
        string Pic_Sales = context.Request.Params["Pic_Sales"].Trim();
        string Tel = context.Request.Params["Tel"].Trim();
        string Cell = context.Request.Params["Cell"].Trim();
        string Charge = context.Request.Params["Charge"].Trim();
        string Charge1 = context.Request.Params["Charge1"].Trim();
        string Route = context.Request.Params["Route"].Trim();
        string Skype = context.Request.Params["Skype"].Trim();
        string Email = context.Request.Params["Email"].Trim();

        if (string.IsNullOrEmpty(Carr))
        {
            Output("Carrier is not Empty.", context);
            return;
        }

        LdExpress.CarrContact obj = new LdExpress.CarrContact();

        obj.ID = ID;
        obj.Load(LdExpress.GV.cn, LdExpress.GV.trans);
        if (obj != null)
        {
            obj.Carr = FormatInput(Carr, obj.Carr, context);
            obj.Port = FormatInput(Port, obj.Port, context);
            obj.Pic_sales = FormatInput(Pic_Sales, obj.Pic_sales, context);
            obj.Tel = FormatInput(Tel, obj.Tel, context);
            obj.Cell = FormatInput(Cell, obj.Cell, context);
            obj.Charge = FormatInput(Charge, obj.Charge, context);
            obj.Charge1 = FormatInput(Charge1, obj.Charge1, context);
            obj.Route = FormatInput(Route, obj.Route, context);
            obj.Skype = FormatInput(Skype, obj.Skype, context, '^');
            obj.Email = FormatInput(Email, obj.Email, context, '^');

            Customers cus = new Customers();
            if (obj.PicID > 0)
            {
                //cus.ID = obj.PicID;
                //cus.Load(LdExpress.GV.cn, LdExpress.GV.trans);
                //cus.HoTen = Pic_Sales;
                //cus.Save(LdExpress.GV.cn, LdExpress.GV.trans);
            }
            else if (obj.Pic_sales != "")
            {
                cus.HoTen = Pic_Sales;
                obj.PicID = cus.Save(LdExpress.GV.cn, LdExpress.GV.trans);
            }

            obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);

            Output("sucessful", context);
        }
    }


    private void UpdateCarrConactV2(HttpContext context)
    {
        string items = context.Request.Params["items"];
        List<LdExpress.CarrContact> udpitems = Newtonsoft.Json.JsonConvert.DeserializeObject<List<LdExpress.CarrContact>>(items);
        string ids = "";
        ids = string.Join(",", udpitems.Select(q => q.ID));
        System.Data.DataSet ds = new System.Data.DataSet();
        string condition = "";
        if (!string.IsNullOrEmpty(ids))
        {
            condition = " and tbl_carrcontact.ID in (" + ids + ")";
        }
        else
        {
            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject("not valid"));
        }
        int nNumOfPages = 0;
        LdExpress.CarrContact.GetList(condition, "", 999, 1, out nNumOfPages, out ds, LdExpress.GV.cn, LdExpress.GV.trans);
        Array arr = udpitems.ToArray();

        if (ds.Tables.Count > 0)
        {
            foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
            {
                long ID = long.Parse(dr["ID"].ToString().Trim());

                LdExpress.CarrContact obj = new LdExpress.CarrContact();
                CarrContact udp = udpitems.First(q => q.ID == ID);

                string Carr = HttpUtility.HtmlDecode(udp.Carr.Trim());
                string Port = HttpUtility.HtmlDecode(udp.Port.Trim());
                string Pic_Sales = HttpUtility.HtmlDecode(udp.Pic_sales.Trim());
                string Tel = HttpUtility.HtmlDecode(udp.Tel.Trim());
                string Cell = HttpUtility.HtmlDecode(udp.Cell.Trim());
                string Charge = HttpUtility.HtmlDecode(udp.Charge.Trim());
                string Charge1 = HttpUtility.HtmlDecode(udp.Charge1.Trim());
                string Route = HttpUtility.HtmlDecode(udp.Route.Trim());
                string Skype = HttpUtility.HtmlDecode(udp.Skype.Trim());
                string Email = HttpUtility.HtmlDecode(udp.Email.Trim());
                string Advantage = HttpUtility.HtmlDecode(udp.Advantage.Trim());

                obj.ID = ID;
                //obj.Description = dr["Description"].ToString();
                //obj.Sort = int.Parse(dr["Sort"].ToString());
                obj.Load(GV.cn, GV.trans);
                if (obj != null)
                {
                    obj.Carr = FormatInput(Carr, obj.Carr, context);
                    obj.Port = FormatInput(Port, obj.Port, context);
                    obj.Pic_sales = FormatInput(Pic_Sales, obj.Pic_sales, context);
                    obj.Tel = FormatInput(Tel, obj.Tel, context);
                    obj.Cell = FormatInput(Cell, obj.Cell, context);
                    obj.Charge = FormatInput(Charge, obj.Charge, context);
                    obj.Charge1 = FormatInput(Charge1, obj.Charge1, context);
                    obj.Route = FormatInput(Route, obj.Route, context);
                    obj.Skype = FormatInput(Skype, obj.Skype, context, '^');
                    obj.Email = FormatInput(Email, obj.Email, context, '^');
                    obj.Advantage = FormatInput(Advantage, obj.Advantage, context, '^');

                    Customers cus = new Customers();
                    if (obj.PicID > 0)
                    {
                    }
                    else if (obj.Pic_sales != "")
                    {
                        cus.HoTen = Pic_Sales;
                        obj.PicID = cus.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                    }

                    obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                }
            }
            Output("sucessful", context);
        }
    }


    private void UpdateColLoader(HttpContext context)
    {
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string Carr = context.Request.Params["Carr"].Trim();
        string Port = context.Request.Params["Port"].Trim();
        string Pic_Sales = context.Request.Params["Pic_Sales"].Trim();
        string Tel = context.Request.Params["Tel"].Trim();
        string Cell = context.Request.Params["Cell"].Trim();
        string Charge = context.Request.Params["Charge"].Trim();
        string Route = context.Request.Params["Route"].Trim();
        string Skype = context.Request.Params["Skype"].Trim();
        string Email = context.Request.Params["Email"].Trim();

        if (string.IsNullOrEmpty(Carr))
        {
            Output("Carrier is not Empty.", context);
            return;
        }

        LdExpress.CoLoader obj = new LdExpress.CoLoader();
        obj.ID = -1;
        if (ID > 0)
        {
            obj.ID = ID;
            obj.Load(LdExpress.GV.cn, LdExpress.GV.trans);
            if (obj != null)
            {
                obj.Carr = Carr;
                obj.Port = Port;
                obj.Pic_sales = Pic_Sales;
                obj.Tel = Tel;
                obj.Cell = Cell;
                obj.Charge = Charge;
                obj.Skype = Skype;
                obj.Email = Email;

                Customers cus = new Customers();
                if (obj.PicID > 0)
                {
                    //cus.ID = obj.PicID;
                    //cus.Load(LdExpress.GV.cn, LdExpress.GV.trans);
                    //cus.HoTen = obj.Pic_sales;
                    //cus.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                }
                else if (obj.Pic_sales != "")
                {
                    cus.HoTen = obj.Pic_sales;
                    obj.PicID = cus.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                }

                obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);

                Output("sucessful", context);
            }
        }
    }
    private void UpdateColLoaderV2(HttpContext context)
    {
        string items = context.Request.Params["items"];
        List<LdExpress.CoLoader> udpitems = Newtonsoft.Json.JsonConvert.DeserializeObject<List<LdExpress.CoLoader>>(items);
        string ids = "";
        ids = string.Join(",", udpitems.Select(q => q.ID));
        System.Data.DataSet ds = new System.Data.DataSet();
        string condition = "";
        if (!string.IsNullOrEmpty(ids))
        {
            condition = " and ID in (" + ids + ")";
        }
        else
        {
            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject("not valid"));
        }
        int nNumOfPages = 0;
        LdExpress.CoLoader.GetList(condition, "", 999, 1, out nNumOfPages, out ds, LdExpress.GV.cn, LdExpress.GV.trans);
        Array arr = udpitems.ToArray();

        if (ds.Tables.Count > 0)
        {
            foreach (System.Data.DataRow dr in ds.Tables[0].Rows)
            {
                long ID = long.Parse(dr["ID"].ToString().Trim());

                LdExpress.CoLoader obj = new LdExpress.CoLoader();
                CoLoader udp = udpitems.First(q => q.ID == ID);

                string Carr = HttpUtility.HtmlDecode(udp.Carr.Trim());
                string Port = HttpUtility.HtmlDecode(udp.Port.Trim());
                string Pic_Sales = HttpUtility.HtmlDecode(udp.Pic_sales.Trim());
                string Tel = HttpUtility.HtmlDecode(udp.Tel.Trim());
                string Cell = HttpUtility.HtmlDecode(udp.Cell.Trim());
                string Charge = HttpUtility.HtmlDecode(udp.Charge.Trim());
                string Skype = HttpUtility.HtmlDecode(udp.Skype.Trim());
                string Email = HttpUtility.HtmlDecode(udp.Email.Trim());

                obj.ID = ID;
                obj.Description = dr["Description"].ToString();
                obj.Sort = int.Parse(dr["Sort"].ToString());
                if (obj != null)
                {
                    obj.Carr = FormatInput(Carr, obj.Carr, context);
                    obj.Port = FormatInput(Port, obj.Port, context);
                    obj.Pic_sales = FormatInput(Pic_Sales, obj.Pic_sales, context);
                    obj.Tel = FormatInput(Tel, obj.Tel, context);
                    obj.Cell = FormatInput(Cell, obj.Cell, context);
                    obj.Charge = FormatInput(Charge, obj.Charge, context);
                    obj.Skype = FormatInput(Skype, obj.Skype, context, '^');
                    obj.Email = FormatInput(Email, obj.Email, context, '^');

                    Customers cus = new Customers();

                    if (obj.PicID > 0)
                    {
                    }
                    else if (obj.Pic_sales != "")
                    {
                        cus.HoTen = Pic_Sales;
                        obj.PicID = cus.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                    }

                    obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);
                }
            }
            Output("sucessful", context);
        }
    }
    private void UpdateLocalCost(HttpContext context)
    {
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string Carr = context.Request.Params["Carr"].Trim();
        if (string.IsNullOrEmpty(Carr))
        {
            Output("Carrier is not Empty.", context);
            return;
        }

        string Export20DC = context.Request.Params["Export20DC"].Trim();
        string Export40DC = context.Request.Params["Export40DC"].Trim();
        string Export20RF = context.Request.Params["Export20RF"].Trim();
        string Export40RF = context.Request.Params["Export40RF"].Trim();
        string Import20DC = context.Request.Params["Import20DC"].Trim();
        string Import40DC = context.Request.Params["Import40DC"].Trim();
        string Import20RF = context.Request.Params["Import20RF"].Trim();
        string Import40RF = context.Request.Params["Import40RF"].Trim();
        string Seal = context.Request.Params["Seal"].Trim();
        string BL = context.Request.Params["BL"].Trim();
        string Telex = context.Request.Params["Telex"].Trim();
        string ENS = context.Request.Params["ENS"].Trim();
        string LatePayMentFEE = context.Request.Params["LatePayMentFEE"].Trim();
        string CIC = context.Request.Params["CIC"].Trim();
        string EBS = context.Request.Params["EBS"].Trim();
        string Remark = context.Request.Params["Remark"].Trim();
        string VSCont = context.Request.Params["VSCont"].Trim();

        LdExpress.LocalCosts obj = new LdExpress.LocalCosts();
        obj.ID = ID;
        obj.Load(LdExpress.GV.cn, LdExpress.GV.trans);
        obj.Export20DC = Export20DC;
        obj.Export40DC = Export40DC;
        obj.Export20RF = Export20RF;
        obj.Export40RF = Export40RF;
        obj.Import20DC = Import20DC;
        obj.Import40DC = Import40DC;
        obj.Import20RF = Import20RF;
        obj.Import40RF = Import40RF;
        obj.Seal = Seal;
        obj.BL = BL;
        obj.Telex = Telex;
        obj.ENS = ENS;
        obj.LatePayMentFEE = LatePayMentFEE;
        obj.CIC = CIC;
        obj.EBS_TEU = EBS;
        obj.Remark = Remark;
        obj.VSCont = VSCont;
        obj.LastUpdate = DateTime.Now;
        obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);

        Output("sucessful", context);
    }
    private void UpdateFunds(HttpContext context)
    {
        string msg = "sucessful";
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string FileNumber = context.Request.Params["FileNumber"].Trim();
        //if (string.IsNullOrEmpty(FileNumber))
        //{
        //    Output("FileNumber is not Empty.", context);
        //    return;
        //}

        string CreateDate = context.Request.Params["CreateDate"].Trim();
        string PaymentSubject = context.Request.Params["PaymentSubject"].Trim();
        string Description = context.Request.Params["Description"].Trim();
        string BankName = context.Request.Params["BankName"].Trim();
        string ReciprocalAccount_Had = context.Request.Params["ReciprocalAccount_Had"].Trim();
        string ReciprocalAccount_Debt = context.Request.Params["ReciprocalAccount_Debt"].Trim();
        string CashVND_Thu = context.Request.Params["CashVND_Thu"].Trim();
        string CashVND_Chi = context.Request.Params["CashVND_Chi"].Trim();
        string CashUSD_Thu = context.Request.Params["CashUSD_Thu"].Trim();
        string CashUSD_Chi = context.Request.Params["CashUSD_Chi"].Trim();
        string BankVND_Thu = context.Request.Params["BankVND_Thu"].Trim();
        string BankVND_Chi = context.Request.Params["BankVND_Chi"].Trim();
        string BankUSD_Thu = context.Request.Params["BankUSD_Thu"].Trim();
        string BankUSD_Chi = context.Request.Params["BankUSD_Chi"].Trim();
        long IdProvince = long.Parse(context.Session["UserProvince"].ToString());
        string SubjectTypeName = context.Request.Params["SubjectTypeName"].Trim();
        LdExpress.Funds obj = new LdExpress.Funds();
        obj.ID = ID;
        obj.Load(LdExpress.GV.cn, LdExpress.GV.trans);

        //Subtractions this value  from Payables report or Receivable repor if filenumber changed
        if (obj.FileNumber != FileNumber && FileNumber.Length > 0)
            Funds.DeleteReceiveAndPaymentReport(obj, GV.cn, GV.trans);

        obj.FileNumber = FileNumber;
        if (!string.IsNullOrEmpty(CreateDate))
            obj.CreateDate = DateTime.Parse(CreateDate, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
        else
            obj.CreateDate = DateTime.Now;
        obj.PaymentSubject = PaymentSubject;
        obj.Description = Description;
        obj.BankName = BankName;
        if (!string.IsNullOrEmpty(ReciprocalAccount_Had))
            obj.ReciprocalAccount_Had = double.Parse(ReciprocalAccount_Had);
        else
            obj.ReciprocalAccount_Had = 0;

        if (!string.IsNullOrEmpty(ReciprocalAccount_Debt))
            obj.ReciprocalAccount_Debt = double.Parse(ReciprocalAccount_Debt);
        else
            obj.ReciprocalAccount_Debt = 0;

        if (!string.IsNullOrEmpty(CashVND_Thu))
            obj.CashVND_Thu = double.Parse(CashVND_Thu);
        else
            obj.CashVND_Thu = 0;

        if (!string.IsNullOrEmpty(CashVND_Chi))
            obj.CashVND_Chi = double.Parse(CashVND_Chi);
        else
            obj.CashVND_Chi = 0;

        if (!string.IsNullOrEmpty(CashUSD_Thu))
            obj.CashUSD_Thu = double.Parse(CashUSD_Thu);
        else
            obj.CashUSD_Thu = 0;

        if (!string.IsNullOrEmpty(CashUSD_Chi))
            obj.CashUSD_Chi = double.Parse(CashUSD_Chi);
        else
            obj.CashUSD_Chi = 0;

        if (!string.IsNullOrEmpty(BankVND_Thu))
            obj.BankVND_Thu = double.Parse(BankVND_Thu);
        else
            obj.BankVND_Thu = 0;

        if (!string.IsNullOrEmpty(BankVND_Chi))
            obj.BankVND_Chi = double.Parse(BankVND_Chi);
        else
            obj.BankVND_Chi = 0;

        if (!string.IsNullOrEmpty(BankUSD_Thu))
            obj.BankUSD_Thu = double.Parse(BankUSD_Thu);
        else
            obj.BankUSD_Thu = 0;

        if (!string.IsNullOrEmpty(BankUSD_Chi))
            obj.BankUSD_Chi = double.Parse(BankUSD_Chi);
        else
            obj.BankUSD_Chi = 0;
        switch (SubjectTypeName.ToLower())
        {
            case Funds.Receive_Customer_Name:
                obj.SubjectType = Funds.Receive_Customer;
                break;
            case Funds.Receive_Other_Name:
                obj.SubjectType = Funds.Receive_Other;
                break;
            case Funds.Payment_Agent_Name:
                obj.SubjectType = Funds.Payment_Agent;
                break;
            case Funds.Payment_Com_Agent_Name:
                obj.SubjectType = Funds.Payment_Com_Agent;
                break;
            case Funds.Payment_Com_Customer_Name:
                obj.SubjectType = Funds.Payment_Com_Customer;
                break;
            case Funds.Payment_Other_Name:
                obj.SubjectType = Funds.Payment_Other;
                break;
            default:
                msg = "Loại hình thanh toán: ' " + SubjectTypeName + "' không có.";
                break;
        }
        if (IdProvince > 0)
        {
            // obj.IdProvince=IdProvince;

            obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);
        }
        Output(msg, context);
    }

    private void UpdateBorrowBook(HttpContext context)
    {
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string FileNumber = context.Request.Params["FileNumber"].Trim();
        //if (string.IsNullOrEmpty(FileNumber))
        //{
        //    Output("FileNumber is not Empty.", context);
        //    return;
        //}

        string CreateDate = context.Request.Params["CreateDate"].Trim();
        string PaymentSubject = context.Request.Params["PaymentSubject"].Trim();
        string Description = context.Request.Params["Description"].Trim();
        string BankName = context.Request.Params["BankName"].Trim();
        string ReciprocalAccount_Had = context.Request.Params["ReciprocalAccount_Had"].Trim();
        string ReciprocalAccount_Debt = context.Request.Params["ReciprocalAccount_Debt"].Trim();
        string Interest = context.Request.Params["Interest"].Trim();
        string Amount_Debt = context.Request.Params["Amount_Debt"].Trim();
        string Amount_Paid = context.Request.Params["Amount_Paid"].Trim();
        string Date_Debt = context.Request.Params["Date_Debt"].Trim();
        string Duration = context.Request.Params["Duration"].Trim();
        long IdProvince = long.Parse(context.Session["UserProvince"].ToString());

        LdExpress.BorrowBook obj = new LdExpress.BorrowBook();
        obj.ID = ID;
        obj.Load(LdExpress.GV.cn, LdExpress.GV.trans);
        obj.FileNumber = FileNumber;
        if (!string.IsNullOrEmpty(CreateDate))
            obj.CreateDate = DateTime.Parse(CreateDate, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
        else
            obj.CreateDate = DateTime.Now;
        if (!string.IsNullOrEmpty(Date_Debt))
            obj.Date_Debt = DateTime.Parse(Date_Debt, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
        else
            obj.CreateDate = DateTime.Now;
        obj.PaymentSubject = PaymentSubject;
        obj.Description = Description;
        obj.BankName = BankName;
        if (!string.IsNullOrEmpty(ReciprocalAccount_Had))
            obj.ReciprocalAccount_Had = double.Parse(ReciprocalAccount_Had);
        else
            obj.ReciprocalAccount_Had = 0;

        if (!string.IsNullOrEmpty(ReciprocalAccount_Debt))
            obj.ReciprocalAccount_Debt = double.Parse(ReciprocalAccount_Debt);
        else
            obj.ReciprocalAccount_Debt = 0;

        if (!string.IsNullOrEmpty(Interest))
            obj.Interest = double.Parse(Interest);
        else
            obj.Interest = 0;
        if (!string.IsNullOrEmpty(Duration))
            obj.Duration = long.Parse(Duration);
        else
            obj.Duration = 0;

        if (!string.IsNullOrEmpty(Amount_Debt))
            obj.Amount_Debt = double.Parse(Amount_Debt);
        else
            obj.Amount_Debt = 0;

        if (!string.IsNullOrEmpty(Amount_Paid))
            obj.Amount_Paid = double.Parse(Amount_Paid);
        else
            obj.Amount_Paid = 0;

        if (IdProvince > 0)
        {
            // obj.IdProvince=IdProvince;

            obj.Save(LdExpress.GV.cn, LdExpress.GV.trans);
        }
        Output("sucessful", context);
    }

    private void UpdateReceivablesReport(HttpContext context)
    {
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string Receivables_Obtained = context.Request.Params["Receivables_Obtained"].Trim();
        string Receivables_Paid_Com = context.Request.Params["Receivables_Paid_Com"].Trim();
        string Receivables_Note = context.Request.Params["Receivables_Note"].Trim();

        string Receivables_Update = context.Request.Params["Receivables_Update"].Trim();
        string Receivables_PaymentMethod = context.Request.Params["Receivables_PaymentMethod"].Trim();

        TotalCostAccounting ttcost = new TotalCostAccounting();
        ttcost.ID = ID;
        ttcost.Load(GV.cn, GV.trans);
        double dReceivables_Obtained = 0;
        double dReceivables_Paid_Com = 0;
        double.TryParse(Receivables_Obtained, out dReceivables_Obtained);
        ttcost.Receivables_Obtained = dReceivables_Obtained;
        double.TryParse(Receivables_Paid_Com, out dReceivables_Paid_Com);
        ttcost.Receivables_Paid_Com = dReceivables_Paid_Com;
        ttcost.Receivables_Note = Receivables_Note;
        ttcost.Receivables_Must_Obtained = ttcost.TotalSellInvoice - dReceivables_Obtained;
        ttcost.Receivables_Must_Paid_Com = ttcost.TotalComCustomer - dReceivables_Paid_Com - ttcost.TotalTaxCustomer;
        if (!string.IsNullOrEmpty(Receivables_Update))
            ttcost.Receivables_Update = DateTime.Parse(Receivables_Update, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
        else
        {
            if (ttcost.Receivables_Update == DateTime.MinValue)
                ttcost.Receivables_Update = DateTime.Now;
            else
                ttcost.Receivables_Update = DateTime.MinValue;
        }
        ttcost.Receivables_PaymentMethod = Receivables_PaymentMethod;
        ttcost.Save(GV.cn, GV.trans);
        Output("sucessful", context);
    }
    private void UpdatePayablesReport(HttpContext context)
    {
        long ID = long.Parse(context.Request.Params["ID"].Trim());
        string Payables_Payment = context.Request.Params["Payables_Payment"].Trim();
        string Payables_Paid_Com = context.Request.Params["Payables_Paid_Com"].Trim();
        string Payables_Note = context.Request.Params["Payables_Note"].Trim();

        string Payables_Update = context.Request.Params["Payables_Update"].Trim();
        string Payables_PaymentMethod = context.Request.Params["Payables_PaymentMethod"].Trim();

        TotalCostAccounting ttcost = new TotalCostAccounting();
        ttcost.ID = ID;
        ttcost.Load(GV.cn, GV.trans);
        double dPayables_Payment = 0;
        double dPayables_Paid_Com = 0;
        double.TryParse(Payables_Paid_Com, out dPayables_Paid_Com);
        double.TryParse(Payables_Payment, out dPayables_Payment);
        ttcost.Payables_Payment = dPayables_Payment;
        ttcost.Payables_Paid_Com = dPayables_Paid_Com;
        ttcost.Payables_Note = Payables_Note;
        ttcost.Payables_Must_Payment = ttcost.TotalBuyInvoice - dPayables_Payment;
        ttcost.Payables_Must_Paid_Com = ttcost.TotalComAgent - dPayables_Paid_Com - ttcost.TotalTaxAgent;
        if (!string.IsNullOrEmpty(Payables_Update))
            ttcost.Payables_Update = DateTime.Parse(Payables_Update, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
        else
        {
            if (ttcost.Payables_Update == DateTime.MinValue)
                ttcost.Payables_Update = DateTime.Now;
            else
                ttcost.Payables_Update = DateTime.MinValue;
        }
        ttcost.Payables_PaymentMethod = Payables_PaymentMethod;
        ttcost.Save(GV.cn, GV.trans);
        Output("sucessful", context);
    }
    private string FormatInput(string newvalue, string oldvalue, HttpContext context, char separator = '_')
    {
        string result = newvalue;
        var user = context.Session["logined_user"];
        var _now = DateTime.Now.ToString("dd-MM-yyyy");
        if ((newvalue != "") && (newvalue != oldvalue.Split(separator)[0]))
            result = newvalue + separator + user + ": " + _now;

        return result;
    }

    private void Output(string message, HttpContext context)
    {
        context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(new { Message = message, Date = DateTime.Now.ToString("dd-MM-yyy") }));
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}